<template>
    <div class="login-container">
        <div class="login-box">
            <div class="login-header">
                <h2>欢迎登录</h2>
                <p class="subtitle">请使用您的账号密码登录系统</p>
            </div>

            <el-form ref="loginFormRef" :model="userInfo" :rules="loginRules" class="login-form">
                <el-form-item prop="account">
                    <el-input v-model="userInfo.account" placeholder="请输入用户名" :prefix-icon="User" size="large" />
                </el-form-item>

                <el-form-item prop="password">
                    <el-input v-model="userInfo.password" type="password" placeholder="请输入密码" :prefix-icon="Lock"
                        size="large" show-password />
                </el-form-item>

                <div class="remember-forgot">
                    <el-checkbox v-model="userInfo.remember">记住我</el-checkbox>
                    <el-link type="primary" :underline="false">忘记密码？</el-link>
                </div>

                <el-button type="primary" class="login-button" size="large" :loading="loading" @click="doLogin">
                    登录
                </el-button>
            </el-form>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'

import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import {useUserStore} from '@/stores/userStore'

const router = useRouter()
const userStore=useUserStore()
const loginFormRef = ref(null)
const loading = ref(false)

const userInfo = ref({
  account: '13800138234',
  password: '123456',
  remember: true
})

const loginRules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
    ]
}

const doLogin = () => {
  const { account, password } = userInfo.value
  // 调用实例方法
  loginFormRef.value.validate(async (valid) => {
    // valid: 所有表单都通过校验  才为true
    console.log(valid)
    // 以valid做为判断条件 如果通过校验才执行登录逻辑
    if (valid) {
      loading.value = true
      try {
        const success = await userStore.getUserInfo({ account, password })
        if (success) {
          router.replace('/')
        }
      } catch (error) {
        console.error('登录失败:', error)
        ElMessage.error('登录失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg, #f5f7fa 0%, #e4e8eb 100%);
}

.login-box {
    width: 420px;
    padding: 40px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.05);
}

.login-header {
    text-align: center;
    margin-bottom: 40px;
}

.login-header h2 {
    font-size: 28px;
    color: #1a1a1a;
    margin-bottom: 8px;
    font-weight: 600;
}

.subtitle {
    color: #666;
    font-size: 14px;
}

.login-form {
    margin-top: 20px;
}

.remember-forgot {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
}

.login-button {
    width: 100%;
    height: 44px;
    font-size: 16px;
    background: #1a1a1a;
    border: none;
    transition: all 0.3s ease;
}

.login-button:hover {
    background: #333;
    transform: translateY(-1px);
}

:deep(.el-input__wrapper) {
    box-shadow: 0 0 0 1px #e4e7ed;
}

:deep(.el-input__wrapper:hover) {
    box-shadow: 0 0 0 1px #c0c4cc;
}

:deep(.el-input__wrapper.is-focus) {
    box-shadow: 0 0 0 1px #1a1a1a;
}
</style>
